var sellerCode="";
$(document).ready(function() {
	//top.UppTabs.operator = top.getCurrentTagTitle();// 设置用户tab的标题值
	$( "#sellers" ).autocomplete({
		source: function( request, response ) {
	        $.ajax({
	          url: contextPath + '/iccard/report/getPackages',
	          dataType: "json",
	          data: {
	            maxRows: 10,
	            keyword: request.term
	          },
	          success: function( data ) {
	        	sellerCode="";
	            response( $.map( data, function( item ) {
	              return {
	                label: item.value+','+item.label,
	                value: item.label
	              };
	            }));
	          }
	        });
	      },
	      minLength: 2,
	      select: function( event, ui ) {
		        sellerCode=ui.item.label;
	      },
	      open: function() {
	        $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
	      },
	      close: function() {
	        $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
	      }
	    });

	doSearch();
});

function doSearch(){
	var dataId = $("#dataId").val();
	var s = $('input[name="beginTime"]').val();
	var e = $('input[name="endTime"]').val();
	$("#data_list").datagrid({
		url : contextPath+"/iccard/report/financial/package/sellerDetail",
		onLoadError : function() {
			uppAjaxError(data);
		},
		onLoadSuccess:function(data){
			$("#export_link").attr('href',contextPath+'/iccard/report/temp/'+data.fileName);
		},
		queryParams : {
			dataId : dataId,
			packageId:sellerCode,
			beginTime:s,
			endTime:e
		},
		onLoadSuccess: function (data) {
			paint();
		},
		columns : [ [ {
			field : 'endCombocardEarningsDetailId',
			title : '',
			hidden : true
		},{
			field : 'daliyEarningsDate',
			title : '日期',
			formatter : function(value, row, index) {
				var d =new Date(Number(value));
				return d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();
			}
		}, {
			field : 'comboCardName',
			title : '套餐名称'
		}, {
			field : 'cardBeginTime',
			title : '套餐开始时间',
			formatter : function(value, row, index) {
				var d =new Date(Number(value));
				return d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();
			}
		}, {
			field : 'cardEndTime',
			title : '套餐结束时间',
			formatter : function(value, row, index) {
				var d =new Date(Number(value));
				return d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();
			}
		}, {
			field : 'comboCardExperseAmount',
			title : '套餐卡消费额'
		}, {
			field : 'comboCardHandleAmount',
			title : '套餐卡应付额'
		},{
			field : 'comboCardSettlementAmount',
			title : '套餐卡结算额'
		},{
			field : 'comboCardRealEarnings',
			title : '实际收益额'
		} ] ],
		autoRowHeight : true,
		fitColumns : true,
		pagination : true,
		nowrap : false,
		singleSelect : true,
		rownumbers : true
	});
//
	
	//汇总
	$.ajax({
        url: contextPath + '/iccard/report/financial/package/sellerDetailSum',
        dataType: "json",
        data: {
			dataId : dataId,
			packageId:sellerCode,
			beginTime:s,
			endTime:e
		},
        success: function( data ) {
        	$("#collect_amount").text("实际收益总额："+data.incomeTotal+"元 ");
        }
      });
	//
}


function paint(){
	var dataId = $("#dataId").val();
	var s = $('input[name="beginTime"]').val();
	var e = $('input[name="endTime"]').val();
	//套餐
	var packages = new Array();
	//chat
	var chat_sales = new Array("",0.0);
	var chat_income = new Array("",0.0);
	$.ajax({
        url: contextPath + '/iccard/report/financial/package/sellerDetailChat',
        dataType: "json",
        async: false,
        data: {
			dataId : dataId,
			beginTime:s,
			endTime:e
		},
        success: function( data ) {
        	packages = data.packages;
        	var t0 = data.sales_rate;
        	var t1 = data.income_rate;
        	for(var i = 0;i <packages.length; i++){
        		chat_sales[i] = new Array("",0.0);
        		chat_sales[i][0] = packages[i];
        		chat_sales[i][1] = t0[i];
        		chat_income[i] = new Array("",0.0);
        		chat_income[i][0] = packages[i];
        		chat_income[i][1] = t1[i];
        	}
        }
      });

	//饼图
    $('#sales_rate').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: '销售额'
        },
        subtitle: {
            text: ''
        },
        tooltip: {
    	    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                }
            }
        },
        series: [{
            type: 'pie',
            name: '占比',
            data: chat_sales
        }]
    });
    
    $('#income_rate').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: '收益额'
        },
        subtitle: {
            text: ''
        },
        tooltip: {
    	    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                }
            }
        },
        series: [{
            type: 'pie',
            name: '占比',
            data: chat_income
        }]
    });
}
